﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using WoltersKluwer.SPChainGang.Core.ExtensionMethods;
using WoltersKluwer.SPChainGang.Core.Util;

namespace WoltersKluwer.SPChainGang.Web.UserControls
{
    public partial class ListPicker : UserControl
    {
        public ListPicker()
        {
            SiteCollectionId = Guid.Empty;
            ListFilter = list => true;
        }

        public Guid SiteCollectionId { get; set;}
        public Func<SPList, bool> ListFilter { get; set; }

        public string Caption
        {
            get
            {
                return SPEncode.HtmlDecode(CaptionLabel.Text);
            }
            set
            {
                CaptionLabel.Text = SPEncode.HtmlEncode(value);
            }
        }

        public Guid SelectedListId
        {
            get
            {
                if (ListDropDown.SelectedItem == null)
                {
                    return Guid.Empty;
                }
                return ListDropDown.SelectedItem.Value.ConvertToGuid();
            }
            set
            {
                var item = ListDropDown.Items.FindByValue(value.ToString());
                if (item != null)
                    item.Selected = true;
            }
        }

        public override void DataBind()
        {
            if (SiteCollectionId != Guid.Empty)
            {
                Guid oldSelectedId = SelectedListId;
                ListDropDown.Items.Clear();
                using (var siteCollection = new SPSite(SiteCollectionId))
                {                 
                        try
                        {
                            using (var web = siteCollection.RootWeb)
                            {
                                foreach (SPList list in web.Lists)
                                {
                                    try
                                    {
                                        if (list != null && ListFilter(list))
                                            ListDropDown.Items.Add(new ListItem(list.Title, list.ID.ToString()));
                                    }
                                    catch (OutOfMemoryException)
                                    {
                                        throw;
                                    }
                                    catch (Exception e)
                                    {
                                        Logger.LogError(Guid.Empty, "ListPicker control: List filtering failed", e); //TODO: refactor the logging subsystem and do not use empty guid for non-job log records
                                    }
                                }
                            }
                        }
                        catch (OutOfMemoryException)
                        {
                            throw;
                        }
                        catch (Exception e)
                        {
                            Logger.LogError(Guid.Empty, "ListPicker control: Web access failed", e);
                        }
                }
                if (Guid.Empty != oldSelectedId)
                {
                    SelectedListId = oldSelectedId;
                }
                     
            }
        }

        public void Update()
        {
            if (IsPostBack)
                ((IPostBackDataHandler)ListDropDown).LoadPostData(ListDropDown.UniqueID, Request.Form);
        }
    }
}